/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xyrule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xyrule.entity.XyRule;
import vip.xiaonuo.modular.xyrule.enums.XyRuleExceptionEnum;
import vip.xiaonuo.modular.xyrule.mapper.XyRuleMapper;
import vip.xiaonuo.modular.xyrule.param.XyRuleParam;
import vip.xiaonuo.modular.xyrule.service.XyRuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 规则动态配置表单service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 17:45:21
 */
@Service
public class XyRuleServiceImpl extends ServiceImpl<XyRuleMapper, XyRule> implements XyRuleService {

    @Override
    public PageResult<XyRule> page(XyRuleParam xyRuleParam) {
        QueryWrapper<XyRule> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyRuleParam)) {

            // 根据规则名称 查询
            if (ObjectUtil.isNotEmpty(xyRuleParam.getRuleName())) {
                queryWrapper.lambda().eq(XyRule::getRuleName, xyRuleParam.getRuleName());
            }
            // 根据规则值 查询
            if (ObjectUtil.isNotEmpty(xyRuleParam.getRuleVal())) {
                queryWrapper.lambda().eq(XyRule::getRuleVal, xyRuleParam.getRuleVal());
            }
            // 根据规则编码 查询
            if (ObjectUtil.isNotEmpty(xyRuleParam.getRuleCode())) {
                queryWrapper.lambda().eq(XyRule::getRuleCode, xyRuleParam.getRuleCode());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyRule> list(XyRuleParam xyRuleParam) {
        return this.list();
    }

    @Override
    public void add(XyRuleParam xyRuleParam) {
        XyRule xyRule = new XyRule();
        BeanUtil.copyProperties(xyRuleParam, xyRule);
        this.save(xyRule);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyRuleParam> xyRuleParamList) {
        xyRuleParamList.forEach(xyRuleParam -> {
            this.removeById(xyRuleParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyRuleParam xyRuleParam) {
        XyRule xyRule = this.queryXyRule(xyRuleParam);
        BeanUtil.copyProperties(xyRuleParam, xyRule);
        this.updateById(xyRule);
    }

    @Override
    public XyRule detail(XyRuleParam xyRuleParam) {
        return this.queryXyRule(xyRuleParam);
    }

    /**
     * 获取规则动态配置表单
     *
     * @author yanwei
     * @date 2023-05-05 17:45:21
     */
    private XyRule queryXyRule(XyRuleParam xyRuleParam) {
        XyRule xyRule = this.getById(xyRuleParam.getId());
        if (ObjectUtil.isNull(xyRule)) {
            throw new ServiceException(XyRuleExceptionEnum.NOT_EXIST);
        }
        return xyRule;
    }

    @Override
    public void export(XyRuleParam xyRuleParam) {
        List<XyRule> list = this.list(xyRuleParam);
        PoiUtil.exportExcelWithStream("SnowyXyRule.xls", XyRule.class, list);
    }

    @Override
    public <T> T  getVal(String id, Class<T> classType){
        return Convert.convert(classType,getById(id).getRuleVal());
    }

}
